<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="行政区划" prop="adminCode">
      <el-cascader
        :options="adminCodeList"
        v-model="selectedOptions"
        :props ="adminCodeDESC"
        filterable
        change-on-select
        expand-trigger="hover"
        @change="handleChange">
      </el-cascader>
    </el-form-item>

    <el-form-item label="账号状态" prop="accountStatus">
      <el-select v-model="dataForm.accountStatus" filterable placeholder="账号状态">
        <el-option
          v-for="item in accountAuditList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间" v-bind:disabled="true"></el-input>
    </el-form-item>
      <el-input v-model="dataForm.adminCode" type="hidden"  placeholder="行政区划"></el-input>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          username: '',
          mobile: '',
          password: '',
          createTime: '',
          accountStatus: '',
          adminCode: ''
        },
        accountAuditList: [
          {value: 'available', label: '正常'},
          {value: 'not_available', label: '注销'},
        ],
        selectedOptions: [],
        adminCodeList: [],
        adminCodeDESC: {value: 'adminCode',label: 'shortName',children: 'children'},
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          adminCode: [
            { required: true, message: '行政区划不能为空', trigger: 'blur' }
          ]
        }

      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.selectedOptions = []
        this.getAdminCodes()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.mobile = data.user.mobile
                this.dataForm.password = data.user.password
                this.dataForm.createTime = data.user.createTime
                this.dataForm.accountStatus = data.user.accountStatus
                this.dataForm.adminCode = data.user.adminCode
                this.selectedOptions = data.adminCodes
              }
            })
          }
        })
      },
      getAdminCodes () {
            this.$http({
              url: this.$http.adornUrl(`/quickService/areacode/adminCodeList`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.adminCodeList = data.adminCodeList
              }
            })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'username': this.dataForm.username,
                'mobile': this.dataForm.mobile,
                'password': this.dataForm.password,
                'createTime': this.dataForm.createTime,
                'accountStatus': this.dataForm.accountStatus,
                'adminCode': this.dataForm.adminCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleChange (data) {
         this.dataForm.adminCode = data[data.length-1]
      }
    }
  }
</script>
